package com.think.teaching.controller;

import com.think.common.controller.BaseController;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.ActionData;
import com.think.teaching.service.impl.ActionDataServiceImpl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 事件数据记录 前端控制器
 * @author 赵兴
 * @since 2020-11-12
 */

@RestController
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "事件数据记录接口")
public class ActionDataController extends BaseController{
	
	private final ActionDataServiceImpl actionDataService;
	
	public ActionDataController(ActionDataServiceImpl actionDataService)
	{
		this.actionDataService = actionDataService;
	}
	
	@ApiOperation("添加新的事件数据")
	@PostMapping("admin/actionData")
	public Result AddActionData(@RequestBody() ActionData data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			
			ActionData actionData = actionDataService.AddActionData(data);
			if (actionData != null) {
				return Result.success(actionData);
			} else {
				return Result.failure(ResultCode.SYSTEM_DATABASE_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("更新事件数据")
	@RequestMapping(value = "admin/actionData", method = RequestMethod.PUT)
	public Result UpdateActionData(@RequestBody() ActionData data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			ActionData actionData =actionDataService.GetActionDataByType(data.getTypeId());
			if(actionData == null || actionData.getId().equals(data.getId())) {
				actionData = actionDataService.UpdateActionData(data);
				if (actionData != null) {
					return Result.success(actionData);
				} else {
					return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
				}
			}
			else {
				return Result.failure(ResultCode.ACTIONDATA_ALREADY_EXIST);
			}
			
		} else
			return result;
	}

	@ApiOperation("删除事件数据")
	@RequestMapping(value = "admin/actionData/{id}", method = RequestMethod.DELETE)
	public Result DeleteActionData(@PathVariable("id") Integer id) {
		ActionData actionData = actionDataService.GetActionData(id,false);
		if (actionData == null) {
			return Result.failure(ResultCode.ACTIONDATA_NOT_EXIST);
		} else {
			if (actionDataService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}

	@ApiOperation("管理员获取事件数据详情")
	@RequestMapping(value = "admin/actionData/{id}", method = RequestMethod.GET)
	public Result AdminGetActionData(@PathVariable("id") Integer id) {
		ActionData actionData = actionDataService.GetActionData(id,false);
		if( actionData== null) {
			return Result.failure(ResultCode.ACTIONDATA_NOT_EXIST);
		}
		else {
			
			return Result.success(actionData);
		}
	}
	
	@ApiOperation("用户获取事件数据详情")
	@RequestMapping(value = "user/actionData/{id}", method = RequestMethod.GET)
	public Result GetActionData(@PathVariable("id") Integer id) {
		ActionData actionData = actionDataService.GetActionData(id,true);
		if( actionData== null) {
			return Result.failure(ResultCode.ACTIONDATA_NOT_EXIST);
		}
		else {
			
			return Result.success(actionData);
		}
	}

	@ApiOperation("管理员修改事件数据状态")
	@RequestMapping(value = "admin/actionData/{id}/{state}", method = RequestMethod.PUT)
	public Result UpdateActionDataState(@PathVariable("id") Integer id, @PathVariable("state") Integer state) {
		ActionData actionData = actionDataService.GetActionData(id,false);
		if( actionData== null) {
			return Result.failure(ResultCode.ACTIONDATA_NOT_EXIST);
		}
		else {
			try {
				actionData = actionDataService.SetActionDataState(id, state);
				return Result.success(actionData);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}

	@ApiOperation("用户搜索事件数据")
	@RequestMapping(value = "user/actionData/search", method = RequestMethod.GET)
	public Result UserSearchActionData(@RequestParam("key") String key,@RequestParam("type")Integer type) {
		return Result.success(actionDataService.ListActionDataSearch(key,type, true));
	}

	@ApiOperation("管理员搜索事件数据")
	@RequestMapping(value = "admin/actionData/search", method = RequestMethod.GET)
	public Result AdminSearchActionData(@RequestParam("key") String key,@RequestParam("type") Integer type) {
		return Result.success(actionDataService.ListActionDataSearch(key,type, false));
	}

	@ApiOperation("管理员获取事件数据列表")
	@RequestMapping(value = "admin/actionDatas/{page}/{count}", method = RequestMethod.GET)
	public Result AdminListActionData(@PathVariable("page") Integer page, @PathVariable("count") Integer count,@RequestParam("type")Integer type) {
		return Result.success(actionDataService.ListActionDataWithType(count, page, type, false));
	}

	@ApiOperation("用户获取事件数据列表")
	@RequestMapping(value = "user/actionDatas/{page}/{count}", method = RequestMethod.GET)
	public Result UserListActionData(@PathVariable("page") Integer page, @PathVariable("count") Integer count,@RequestParam("type")Integer type) {
		return Result.success(actionDataService.ListActionDataWithType(count, page, type, true));
	}
}
